//package com.huyun.web.concurrency.atomic;
//
//
//import com.huyun.users.model.Users;
//import com.huyun.web.concurrency.annoations.NotThreadSafe;
//import lombok.extern.slf4j.Slf4j;
//
//import java.util.concurrent.CountDownLatch;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.Semaphore;
//import java.util.concurrent.atomic.AtomicReference;
//
///**
// * @author huyunqiang
// * @date 2019/7/5 15:50
// */
//@Slf4j
//@NotThreadSafe
//public class AtomicReferenceTest {
//    // 请求总数
//    public static int clientTotal = 1000;
//
//    // 同时并发执行的线程数
//    public static int threadTotal = 500;
//
//    public static Users person = new Users();
//
//
//    public static AtomicReference<Users> personAtomicReference = new AtomicReference(person);
//
//    public static void main(String[] args) throws Exception {
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        final Semaphore semaphore = new Semaphore(threadTotal);
//        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
//        for (int i = 0; i < clientTotal; i++) {
//            final int result = i;
//            executorService.execute(() -> {
//                try {
//                    semaphore.acquire();
//                    modify(result);
//                    semaphore.release();
//                } catch (Exception e) {
//                    log.error("exception", e);
//                }
//                countDownLatch.countDown();
//            });
//        }
//        countDownLatch.await();
//        executorService.shutdown();
//        log.info("name:{},age:{}", personAtomicReference.get().getUserId(), personAtomicReference.get().getMobile());
//    }
//
//    //如果线程安全的话，age的值和name的值是一致的
//    //如果线程不安全的话，age的值和name是不一样的。
//    private static void modify(int i) {
//        personAtomicReference.get().setUserId(personAtomicReference.get().getUserId() + i);
//        personAtomicReference.get().setMobile(personAtomicReference.get().getMobile() + i);
//    }
//}
